package com.foosun.core.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foosun.core.base.BaseDao;
import com.foosun.core.entity.Resource;
import com.foosun.core.entity.ResourceGroup;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created by Nelin on 14-2-21.
 */
@Repository
public class ResourceDao extends BaseDao<Resource> {


    @javax.annotation.Resource
    private ResourceGroupDao resourceGroupDao;

    public List<Resource> getResourceByIds(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            return new ArrayList<Resource>();
        }
        String id[] = ids.split(",");
        List<Integer> idList = new ArrayList<Integer>();
        for (String i : id) {
            idList.add(Integer.parseInt(i));
        }
        return this.findByIds(idList);
    }

    public String getResourceNamesByIds(String ids) throws Exception {
        List<Resource> resources = this.getResourceByIds(ids);
        String result = "";
        for (Resource resource : resources) {
            if (resource.getEnable() != null && resource.getEnable()) continue;
            result += "|" + resource.getName();
        }
        return result.substring(1);
    }

    public JSONObject getResourceMapByIds(String ids) throws Exception {
        JSONObject result = new JSONObject();
        List<Resource> resources = this.getResourceByIds(ids);
        for (Resource resource : resources) {

            //不是菜单也不会出现
            if(resource.getIsMenu() == null || !resource.getIsMenu()) continue;

            //被禁用的资源不会出现
            if (resource.getEnable() != null && resource.getEnable()) continue;
            JSONObject g = result.getJSONObject(resource.getGroupId().toString());
            JSONArray array;
            if (g == null) {
                g = new JSONObject();
                ResourceGroup group = resourceGroupDao.findById(resource.getGroupId());
                if (group == null || group.getEnable() == true) continue;//被禁用的组不会出现
                if (group.getIsMenu() == null || !group.getIsMenu()) continue;//不是菜单项也不会出现
                g.put("group", group);
                array = new JSONArray();
                g.put("items", array);
                result.put(resource.getGroupId().toString(), g);
            } else {
                array = g.getJSONArray("items");
            }
            array.add(resource);
        }

        return result;
    }


    /**
     * 对资源集合result中的分组和资源进行排序
     *
     * @param result
     */
    public LinkedHashMap<String, Object> sortResource(JSONObject result) {
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        Set<Map.Entry<String, Object>> groupMap = result.entrySet();
        List<Map.Entry<String, Object>> sortList = new LinkedList<Map.Entry<String, Object>>();
        for (Map.Entry<String, Object> entry : groupMap) {
            sortList.add(entry);
            JSONArray array = ((JSONObject) entry.getValue()).getJSONArray("items");
            Collections.sort(array, comparatorResource);
        }
        Collections.sort(sortList, comparatorGroup);
        for (Map.Entry<String, Object> entry : sortList) {
            r.put(entry.getKey(), entry.getValue());
        }
        return r;
    }

    Comparator<Object> comparatorResource = new Comparator<Object>() {
        public int compare(Object s1, Object s2) {
            Resource r1 = (Resource) s1;
            Resource r2 = (Resource) s2;
            //根据order排序
            int o1, o2;
            o1 = r1.getOrder() == null ? 0 : r1.getOrder();
            o2 = r2.getOrder() == null ? 0 : r2.getOrder();
            return o1 - o2;
        }
    };
    Comparator<Map.Entry<String, Object>> comparatorGroup = new Comparator<Map.Entry<String, Object>>() {
        public int compare(Map.Entry<String, Object> s1, Map.Entry<String, Object> s2) {
            ResourceGroup g1 = (ResourceGroup) ((JSONObject) s1.getValue()).get("group");
            ResourceGroup g2 = (ResourceGroup) ((JSONObject) s2.getValue()).get("group");
            //根据order排序
            int o1, o2;
            o1 = g1 == null ? 0 : g1.getOrder() == null ? 0 : g1.getOrder();
            o2 = g2 == null ? 0 : g2.getOrder() == null ? 0 : g2.getOrder();
            return o1 - o2;
        }
    };

    public LinkedHashMap<String, Object> getResourceMap(String scope) throws Exception {
        JSONObject resources = this.getResourceMapByIds(scope);
        return this.sortResource(resources);
    }
}
